package com.example.shopping.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.shopping.common.consts.MessageEnum;
import com.example.shopping.dao.FavorDao;
import com.example.shopping.dao.GoodsDao;
import com.example.shopping.pojo.domain.FavorBean;
import com.example.shopping.pojo.domain.GoodsBean;
import com.example.shopping.pojo.vo.GoodsVo;
import com.example.shopping.service.FavorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Dream
 * @version 1.0
 * @date 2020/11/9 13:03
 */

@Service
public class FavorServiceImpl extends ServiceImpl<FavorDao,FavorBean> implements FavorService {
    @Autowired
    private FavorDao favorDao;
    @Autowired
    private GoodsDao goodsDao;

    @Override
    public List<GoodsVo> getByxh(String userXh) {
        return favorDao.getByXh(userXh);
    }

    @Override
    public String addFavor(FavorBean favorBean) {
        FavorBean favor=favorDao.getBy(favorBean.getUserXh(),favorBean.getGoodsId());
        GoodsBean goodsBean=goodsDao.getById(favorBean.getGoodsId());
        //收藏+1
        goodsBean.setFavorites(goodsBean.getFavorites()+1);
        goodsDao.updateGoods(goodsBean);
        if(favor==null){
            favorDao.addFavor(favorBean);
            return MessageEnum.ADD_FAVOR_SUCCESS;
        }else{
            return MessageEnum.FAVOR_EXIST;
        }


    }
}
